package com.android.aishangauction.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.aishangauction.R;
import com.android.aishangauction.app.BaseActivity;
import com.android.aishangauction.cache.DiskLruCacheHelper;
import com.android.aishangauction.fragment.group.Fragment_Orderts_History2;
import com.android.aishangauction.fragment.group.Fragment_TabBrandList;
import com.android.aishangauction.fragment.group.Fragment_TabKillList;
import com.android.aishangauction.fragment.group.Fragment_TabNationList;
import com.android.aishangauction.fragment.group.Fragment_TabSearchList;
import com.android.aishangauction.fragment.member.Fragment_Mine_CollectGoods;
import com.android.aishangauction.fragment.member.Fragment_Mine_CollectStore;
import com.android.aishangauction.fragment.member.Fragment_Mine_Coupon;
import com.android.aishangauction.fragment.member.Fragment_Mine_DirectBuy_Order;
import com.android.aishangauction.fragment.member.Fragment_Mine_MerVoucher;
import com.android.aishangauction.fragment.member.Fragment_Mine_Message;
import com.android.aishangauction.fragment.member.Fragment_Mine_Order;
import com.android.aishangauction.fragment.member.Fragment_Mine_Team;
import com.android.aishangauction.model.MainTabData;
import com.android.aishangauction.model.group.MainSearchDataTotal;
import com.android.aishangauction.model.group.MainSearchSortData;
import com.android.aishangauction.model.group.SortBrandData;
import com.android.aishangauction.model.group.SortNationData;
import com.android.aishangauction.utils.ConstantsUrl;
import com.android.aishangauction.utils.GsonUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainTab extends BaseActivity {
    @BindView(R.id.sort_tab)
    TabLayout sortTab;
    @BindView(R.id.sort_list)
    ImageView sortList;
    @BindView(R.id.sort_viewpager)
    ViewPager sortViewpager;
    @BindView(R.id.click_resetnetwork_msg)
    TextView clickResetnetworkMsg;
    @BindView(R.id.click_resetnetwork)
    LinearLayout clickResetnetwork;
    @BindView(R.id.progress)
    RelativeLayout progress;
    @BindView(R.id.no_data_tv)
    TextView noDataTv;
    @BindView(R.id.no_data)
    RelativeLayout noData;
    @BindView(R.id.gridView_sort)
    GridView gridView_sort;
    @BindView(R.id.pop_sort)
    LinearLayout popSort;
    private MyFragmentPagerAdapter pagerAdapter;
    private MyFragmentPagerAdapter4 pagerAdapter4;
    private MainTab mcontext;
    private int haitaocity;
    private int tabsort_type;
    public static MainTab intance = null;
    public boolean isupdate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maintab);
        ButterKnife.bind(this);
        mcontext = MainTab.this;
        intance = this;
        tabsort_type = getIntent().getIntExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TAB_SORT_SHOW_TYPE, 0);
        haitaocity = getIntent().getIntExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TAB_SORT_SHOW_ID, 0);
        switch (tabsort_type) {
            case 0:
                initTitle(0, "品牌团");
                break;
            case 1:
                initTitle(0, "海淘团");
                break;
            case 2:
                initTitle(0, "商品列表");
                break;
            case 3:
                initTitle(0, "秒杀团");
                break;
            case 4:
                initTitle(0, getString(R.string.app_mine_keyname));
                break;
            case 5:
                initTitle(0, "我的团");
                break;
            case 6:
                initTitle(0, "我的收藏");
                break;
            case 7:
                initTitle(0, "我的优惠券");
                break;
            case 8:
                initTitle(0, "我的订单");
                break;
            case 9:
                initTitle(0,"我的商家券");
                break;
            case 10:
                initTitle(0,"消息");
                break;
            case 11:
                initTitle(0,"我的晒单");
                break;

        }
        GETSortData();


    }

    public ArrayList<SortBrandData.DataBean.ListBean> mSortListdata_dapai = new ArrayList<>();
    public ArrayList<SortNationData.Nationdata> mSortListdata_nation = new ArrayList<>();
    public ArrayList<MainSearchDataTotal> mSortListdata_search = new ArrayList<>();
    private List<MainSearchSortData.DataBean> mListSort = new ArrayList<>();
    private ArrayList<MainTabData> names = new ArrayList<>();
    private int lastPosition = 0;

    private void GETSortData() {

        try {
            DiskLruCacheHelper helper = new DiskLruCacheHelper(mcontext);
            switch (tabsort_type) {
                case 0:
                    sortTab.setTabMode(TabLayout.MODE_SCROLLABLE);
                    mSortListdata_dapai.clear();
                    String cache_dapai = helper.getAsString(ConstantsUrl.CACHE_HOME_SORTDATA_DAPAI);
                    if (!TextUtils.isEmpty(cache_dapai)) {
                        SortBrandData banner = GsonUtils.parseJSON(cache_dapai, SortBrandData.class);
                        mSortListdata_dapai.addAll(banner.getData().getList());
                        progress.setVisibility(View.GONE);
                    }
                    break;
                case 1:
                    sortTab.setTabMode(TabLayout.MODE_SCROLLABLE);
                    mSortListdata_nation.clear();
                    String cache_nation = helper.getAsString(ConstantsUrl.CACHE_HOME_SORTDATA_NATION);
                    if (!TextUtils.isEmpty(cache_nation)) {
                        SortNationData banner = GsonUtils.parseJSON(cache_nation, SortNationData.class);
                        mSortListdata_nation.addAll(banner.getData());
                        progress.setVisibility(View.GONE);
                    }
                    break;
                case 2:
                    sortTab.setTabMode(TabLayout.MODE_SCROLLABLE);
                    String cache_sort = helper.getAsString(ConstantsUrl.CACHE_HOME_SORTDATA_SEARCH);
                    String sort_type = getIntent().getStringExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TAB_SORT_SHOW_TYPE_SERACH);
                    mSortListdata_search.add(new MainSearchDataTotal(sort_type, "全部"));
                    switch (sort_type) {
                        case "0":
                            if (!TextUtils.isEmpty(cache_sort)) {
                                MainSearchSortData banner = GsonUtils.parseJSON(cache_sort, MainSearchSortData.class);
                                mListSort.add(new MainSearchSortData.DataBean(sort_type, "全部"));
                                mListSort.addAll(banner.getData());
                                for (int i = 0; i < banner.getData().size(); i++) {

                                    mSortListdata_search.add(new MainSearchDataTotal(banner.getData().get(i).getId(), banner.getData().get(i).getCatname()));
                                }

                                progress.setVisibility(View.GONE);
                            }
                            break;
                        default:
                            if (!TextUtils.isEmpty(cache_sort)) {
                                MainSearchSortData banner = GsonUtils.parseJSON(cache_sort, MainSearchSortData.class);
                                for (int i = 0; i < banner.getData().size(); i++) {
                                    if (banner.getData().get(i).getId().equals(sort_type)) {
                                        List<MainSearchSortData.DataBean.SubBeanX> sub = banner.getData().get(i).getSub();
                                        for (int j = 0; j < sub.size(); j++) {

                                            mSortListdata_search.add(new MainSearchDataTotal(sub.get(j).getId(), sub.get(j).getCatname()));
                                        }
                                    }

                                }

                                progress.setVisibility(View.GONE);
                            }
                            break;

                    }


                    sortList.setVisibility(View.VISIBLE);


                    break;
                case 3:
                    sortTab.setTabMode(TabLayout.MODE_FIXED);
                    progress.setVisibility(View.GONE);
                    names.add(new MainTabData("正在抢购", "&time=now"));
                    names.add(new MainTabData("即将开始", "&time=well"));

                    break;
                case 4:
                    progress.setVisibility(View.GONE);
                    sortTab.setTabMode(TabLayout.MODE_FIXED);
                    names.add(new MainTabData("全部", "0"));
                    names.add(new MainTabData(getString(R.string.app_mine_keyname1), "100"));
                    names.add(new MainTabData(getString(R.string.app_mine_keyname2), "1"));
                    names.add(new MainTabData(getString(R.string.app_mine_keyname3), "2"));
                    names.add(new MainTabData("待付款", "3"));
                    break;
                case 5:
                    progress.setVisibility(View.GONE);
                    sortTab.setTabMode(TabLayout.MODE_FIXED);
                    names.add(new MainTabData("全部", "0"));
                    names.add(new MainTabData("拼团中", "1"));
                    names.add(new MainTabData("已成团", "10"));
                    names.add(new MainTabData("拼团失败", "2"));
                    break;
                case 6:
                    progress.setVisibility(View.GONE);
                    sortTab.setTabMode(TabLayout.MODE_FIXED);
                    names.add(new MainTabData("商品", "0"));
                    names.add(new MainTabData("店铺", "1"));
                    break;
                case 7:
                    progress.setVisibility(View.GONE);
                    sortTab.setTabMode(TabLayout.MODE_FIXED);
                    names.add(new MainTabData("未使用", "1"));
                    names.add(new MainTabData("已使用/已过期", "2|3"));
                    break;
                case 8:
                    progress.setVisibility(View.GONE);
                    sortTab.setTabMode(TabLayout.MODE_SCROLLABLE);
                    names.add(new MainTabData("全部", "0"));
                    names.add(new MainTabData("待付款", "100"));
                    names.add(new MainTabData("待发货", "101"));
                    names.add(new MainTabData("待收货", "102"));
                    names.add(new MainTabData("已完成", "110"));
                    names.add(new MainTabData("取消", "111"));
                    break;
                case 9:
                    progress.setVisibility(View.GONE);
                    sortTab.setTabMode(TabLayout.MODE_SCROLLABLE);
                    names.add(new MainTabData("全部", "0"));
                    names.add(new MainTabData("未使用", "1"));
                    names.add(new MainTabData("已使用", "2"));
                    names.add(new MainTabData("已过期", "3"));
                    break;
                case 10:
                    sortTab.setTabMode(TabLayout.MODE_FIXED);
                    progress.setVisibility(View.GONE);
                    names.add(new MainTabData("消息", "0"));
                    names.add(new MainTabData("公告", "1"));
                    break;
                case 11:
                    sortTab.setTabMode(TabLayout.MODE_FIXED);
                    progress.setVisibility(View.GONE);
                    names.add(new MainTabData("我的晒单", "0"));
                    names.add(new MainTabData("待晒单", "1"));
                    break;
            }
            initUI();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    private void initUI() {
        switch (tabsort_type) {
            case 4:
                pagerAdapter4 = new MyFragmentPagerAdapter4(getSupportFragmentManager());
                sortViewpager.setAdapter(pagerAdapter4);
                break;
            default:
                pagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
                sortViewpager.setAdapter(pagerAdapter);
                break;
        }
        sortTab.setupWithViewPager(sortViewpager);
        sortViewpager.setCurrentItem(haitaocity);
        sortViewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                lastPosition = position;
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {


            }
        });


    }


    @OnClick({R.id.sort_list, R.id.click_resetnetwork_refresh, R.id.close_img, R.id.close})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.sort_list:
                popSort.setVisibility(View.VISIBLE);
                final GridSearchSortAdapt adapter_gridsort = new GridSearchSortAdapt(mSortListdata_search);
                gridView_sort.setAdapter(adapter_gridsort);

                gridView_sort.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        adapter_gridsort.setSeclection(position);
                        adapter_gridsort.notifyDataSetChanged();
                        sortViewpager.setCurrentItem(position);
                        popSort.setVisibility(View.GONE);
                    }
                });


                break;
            case R.id.click_resetnetwork_refresh:
                break;
            case R.id.close_img:
                popSort.setVisibility(View.GONE);
                break;
            case R.id.close:
                popSort.setVisibility(View.GONE);
                break;
        }
    }
    class MyFragmentPagerAdapter4 extends FragmentStatePagerAdapter {

        private HashMap<String,Fragment> fragment_mine_order = new HashMap<>();
        public MyFragmentPagerAdapter4(FragmentManager fm) {
            super(fm);

        }

        @Override
        public Fragment getItem(int position) {
            return Fragment_Mine_Order.newInstance(names.get(position).getId());

        }

        @Override
        public int getCount() {
            return names.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return names.get(position).getName();
        }
    }

    class MyFragmentPagerAdapter extends FragmentPagerAdapter

    {

        private HashMap<String,Fragment> fragment_mine_order = new HashMap<>();
        public MyFragmentPagerAdapter(FragmentManager fm) {
            super(fm);

        }

        @Override
        public Fragment getItem(int position) {
            switch (tabsort_type) {
                case 0:
                    return Fragment_TabBrandList.newInstance(mSortListdata_dapai.get(position).getId(), mSortListdata_dapai.get(position).getEnd_time());
                case 1:
                    return Fragment_TabNationList.newInstance(mSortListdata_nation.get(position).getId(), mSortListdata_nation.get(position).getThumb());
                case 2:
                    return Fragment_TabSearchList.newInstance(position, mSortListdata_search.get(position).getId(), mListSort);
                case 3:
                    return Fragment_TabKillList.newInstance(names.get(position).getId());
                case 5:
                    return Fragment_Mine_Team.newInstance(names.get(position).getId());
                case 6:
                    if (position == 0) {
                        return Fragment_Mine_CollectGoods.newInstance();
                    } else {
                        return Fragment_Mine_CollectStore.newInstance();
                    }
                case 7:
                    return Fragment_Mine_Coupon.newInstance(names.get(position).getId());
                case 8:
                    return Fragment_Mine_DirectBuy_Order.newInstance(names.get(position).getId());
                case 9:
                    return Fragment_Mine_MerVoucher.newInstance(names.get(position).getId());
                case 10:
                    return Fragment_Mine_Message.newInstance(names.get(position).getId());
                case 11:
                    if (position == 0) {
                        return Fragment_Orderts_History2.newInstance();
                    } else {
                        return Fragment_Mine_Order.newInstance("4");
                    }
            }
            return null;
        }

        @Override
        public int getCount() {
            switch (tabsort_type) {
                case 0:
                    return mSortListdata_dapai.size();

                case 1:
                    return mSortListdata_nation.size();
                case 2:
                    return mSortListdata_search.size();
                default:
                    return names.size();
            }

        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (tabsort_type) {
                case 0:
                    return mSortListdata_dapai.get(position).getCatname();

                case 1:
                    return mSortListdata_nation.get(position).getCatname();
                case 2:
                    return mSortListdata_search.get(position).getCatname();
                default:
                    return names.get(position).getName();
            }

        }
    }


    private class GridSearchSortAdapt extends BaseAdapter {


        private ArrayList<MainSearchDataTotal> listdata;


        public GridSearchSortAdapt(ArrayList<MainSearchDataTotal> listdata) {
            this.listdata = listdata;
        }

        public void setSeclection(int position) {
            lastPosition = position;
        }

        @Override
        public int getCount() {
            return listdata.size();
        }

        @Override
        public BaseObject getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            View view = getLayoutInflater().inflate(R.layout.item_main_type_sort, null);
            TextView tv_sort = (TextView) view.findViewById(R.id.tv_sort);

            tv_sort.setText(listdata.get(position).getCatname());

            if (lastPosition == position) {
                tv_sort.setTextColor(Color.parseColor("#FF2741"));
            } else {
                tv_sort.setTextColor(Color.parseColor("#5a5a5a"));
            }

            return view;
        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (isupdate) {
            Intent data = new Intent();
            setResult(RESULT_OK, data);
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);

    }

    @OnClick(R.id.main_top_back)
    public void onClick() {
        if (isupdate) {
            Intent data = new Intent();
            setResult(RESULT_OK, data);
            finish();
        }
    }
}